<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
if(!checkSession())
{
	function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
else
{
	if(isset($_REQUEST['id']))
	{
		$USER_PROFILE = new User('id', $_REQUEST['id']);
		$page_url = "index.php?action=profile&id=" . $_REQUEST['id'] . "&sub=stats";
	}
	else
	{
		$USER_PROFILE = $USER;
		$page_url = "index.php?action=profile&sub=stats";
	}
}
include('inc/functions_blog.php');
//include('Charts/Includes/FusionCharts.php');
$blogs = getTrainingList(" WHERE user_id='" . $USER_PROFILE->getId() . "'");
$dateformat = $CONFIG['date_format'];
$time_now = time();
$blog['today'] = date('d', $time_now);
$blog['tomonth'] = date('m', $time_now);
$blog['toyear'] = date('Y', $time_now);

$blog['fromday'] = date('d', $time_now);
$blog['frommonth'] = date('m', $time_now);
$blog['fromyear'] = date('Y', $time_now);

if(isset($_POST['select']))
{
	
	$blog['fromday']=$_POST['fromday'];
	$blog['frommonth']=$_POST['frommonth'];
	$blog['fromyear']=$_POST['fromyear'];
	$fromdate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['frommonth'], $blog['fromday'], $blog['fromyear']);
	
	$blog['today']=$_POST['today'];
	$blog['tomonth']=$_POST['tomonth'];
	$blog['toyear']=$_POST['toyear'];
	
}

?>

<!--
<SCRIPT LANGUAGE="Javascript" SRC="Charts/FusionCharts.js"></SCRIPT>
-->
<script type="text/javascript" src="./Highcharts/js/highcharts.js"></script>

<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
         &gt; <a href="index.php?action=profile&id=<?php echo $USER_PROFILE->getId(); ?>"><?php echo ucf($USER_PROFILE->getFirstname()) . " " . ucf($USER_PROFILE->getLastname()); ?></a>
		&gt; <a href="index.php?action=profile&sub=stats&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('stats'); ?>'><?php echo translate('stats'); ?></a>
        &gt; <a href="index.php?action=profile&sub=stats_bw&id=<?php echo $USER_PROFILE->getId(); ?>" title='<?php echo translate('bodyweight'); ?>'><?php echo translate('bodyweight'); ?></a>
</div>
<?php 

if(count($blogs) > 0)
{

?>
<br />
<h2><?php echo translate('bodyweight'); ?></h2>
<br />
<?php echo translate('bodyweight_subtext'); ?>
<br /><br />
<form method="post" action="index.php?action=profile&sub=stats_bw&id=<?php echo $USER_PROFILE->getId(); ?>"> 
	<table border="0">
    <tr>
    <td>
    <b><?php echo translate('from'); ?></b>	<select id="day" name="fromday">
            <?php
            for($i=1; $i<=31; $i++)
            {
                if($i == $blog['fromday'])
                {
                    echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
                }
            }
            ?>
        </select>
        
        <select id="month" name="frommonth">
            <?php
            foreach($MONTHS as $k => $v)
            {
                if(($k+1) == $blog['frommonth'])
                {
                    echo "<option value=\"" . ($k+1) . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . ($k+1) . "\">" . ucfirst(translate($v)) . "</option>\n";
                }
            }
            ?>
        </select>
    
        <select id="year" name="fromyear">
            <?php
            for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 11); $i--)
            {
                if($i == $blog['fromyear'])
                {
                    echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
                }
            }
            ?>
        </select> 
    </td>
        <td>
        <b><?php echo translate('to'); ?></b>
        <select id="day" name="today">
        <?php
        for($i=1; $i<=31; $i++)
        {
            if($i == $blog['today'])
            {
                echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
            }
            else
            {
                echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
            }
        }
        ?>
    	</select>
        
        <select id="month" name="tomonth">
            <?php
            foreach($MONTHS as $k => $v)
            {
                if(($k+1) == $blog['tomonth'])
                {
                    echo "<option value=\"" . ($k+1) . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . ($k+1) . "\">" . ucfirst(translate($v)) . "</option>\n";
                }
            }
            ?>
        </select>
        
        <select id="year" name="toyear">
            <?php
            for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 11); $i--)
            {
                if($i == $blog['toyear'])
                {
                    echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
                }
                else
                {
                    echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
                }
            }
            ?>
        </select>     
        <td>
        <input type="hidden" name="user_id" value="<?php echo $USER_PROFILE->getId(); ?>"  />
        <input type="submit" style="font-weight:bold;" name="select" value=" <?php echo ucf(translate('show')); ?> "  />
        </td>
    </table>
</form>

<?php 
if(isset($_POST['select']))
{
	
	$blog['fromday']=$_POST['fromday'];
	$blog['frommonth']=$_POST['frommonth'];
	$blog['fromyear']=$_POST['fromyear'];
	$fromdate = mktime(date('H', 00), date('i', 00), date('s', 00), $blog['frommonth'], $blog['fromday'], $blog['fromyear']);
	
	$blog['today']=$_POST['today'];
	$blog['tomonth']=$_POST['tomonth'];
	$blog['toyear']=$_POST['toyear'];
	$todate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['tomonth'], $blog['today'], $blog['toyear']);

	$user_id = $_POST['user_id'];

echo "<br />";
	
	// Get all the data
	$result = mysql_query("SELECT DISTINCT
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

$num = mysql_num_rows($result);
$i=1;
//$strXML = "<chart caption='" . ucf(translate('bodyweight')) . "' subCaption='" . ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId()) . "' showBorder='1' formatNumberScale='0' numberSuffix='". $w_metric ."' baseFont='Arial' exportEnabled='1' exportAtClient='0' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank' exportFileName='" . ucf(translate('bodyweight')) . "'>";

	if($i < $num)
	{ 
		//	$catstrXML = "<categories>"; 
		//	$datastrXML = "<dataset seriesName='". translate('bodyweight') ." " . translate('for') . " " . getNameFromId($USER_PROFILE->getId()) . "'>"; 
	 		$date_array = array();	
			$weight_array = array();
			
			while($row = mysql_fetch_array($result)){ 
				//   $catstrXML .= "<category label='" . formatDate($row['date'], $dateformat) . "' />";   
					$date_array[] = formatDate($row['date'], $dateformat);           
				   if($w_metric == "kg"){ 
					 //  $datastrXML .= "<set value='" . $row['bodyweight'] . "' />"; 
					  $weight_array[] = $row['bodyweight'];
				   }else{ 
					  // $datastrXML .= "<set value='" . r_nr(calcFromKg($row['bodyweight'])) . "' />"; 
					   $weight_array[] = r_nr(calcFromKg($row['bodyweight']));
				   } 
			}
		$max = max($weight_array) + 1;
		$min = min($weight_array) - 1;
		
		foreach($date_array as &$val){
			$val = "'".$val."'";
		}
		$cat = "[". implode(',',$date_array) ."]";
		$weight = "[". implode(',',$weight_array) ."]"; 
		
		
	?>
<div id="container_graph" style="width: auto; height: auto; margin: 0 auto"></div>
	
	<script type="text/javascript">	
		var kg_title = '<?php echo translate("Kg");?>';
		var min = <?php echo $min;?>;
		var max = <?php echo $max;?>;
		$(document).ready(function() {
			chart = new Highcharts.Chart({
					chart: {
						renderTo: 'container_graph',
						defaultSeriesType: 'line'
					},
					title: {
						text: '<?php echo ucf(translate('bodyweight')). " "  ;?>'
					},
					subtitle: {
						text: '<?php echo ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId());?>'
					},
					xAxis: {
						categories: eval(<?php echo $cat;?>), 
						title: {
							text: '<?php echo translate("day_title");?>'
						},
						labels: {
							rotation: 90,
							y: 40
						}
					},
					yAxis: {
						min: min,
						max:max,
						title: {
							text: '<?php echo translate("weight_unit");?>',
							align: 'high'
						}
					},
					tooltip: {
						formatter: function() {
							return '<b>'+this.x+'</b><br />'+this.y + ' '+kg_title;
						}
					},
					plotOptions: {
						bar: {
							dataLabels: {
								enabled: true
							}
						}
					},
					legend: {
						enabled: false
					},
					credits: {
						enabled: false
					},
			        series: [{
						name: '',
						data: eval(<?php echo $weight;?>)
					}]
				});	
			});
		
	</script>
	
	<?php 		
		//	$catstrXML .= "</categories>"; 
		//	$datastrXML .= "</dataset>";
		//	$strXML .= $catstrXML . $datastrXML . "</chart>";  
	 
	 
		//	echo renderChart("Charts/ZoomLine.swf", "", $strXML, "".ucf(translate('bodyweight'))."", 900, 400, false, true); 
			
			echo "<div class='fixed'> 
								</div> 
							</div> 
							</div> 
						<div class='fixed'> 
						</div> 
						<div id='footer'> 
						<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
							<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
						<br />
				
				<a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				<img alt='Creative Commons License' style='border-width:0' 
				src='http://i.creativecommons.org/l/by-nc/3.0/80x15.png' /></a>
				<br />This work is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-nc/3.0/'>
				Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.	
					</div>		
							</div>"; 
				exit; 
	 
	} 
	else
	{
		echo translate('noresultsbw');
		echo "<div class='fixed'>
						</div>
					</div>
					</div>
				<div class='fixed'>
				</div>
				<div id='footer'>
				<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
					<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
					</div>";
		exit;
	} 		
}

}
else
{
  echo "<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><h2>" . translate('addanewtrainingtostart') . "</h2></center>";
}
?>